package autoteam.ui.tabs;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTable;

import autoteam.tables.UserTableModel;
import autoteam.ui.AutoTeamTablePanel;
import autoteam.ui.dialogs.UserCreator;
import autoteam.ui.dialogs.UserEditor;

public class UsersTab extends TabPanel
{
	private static final long serialVersionUID = -8587525185694836579L;

	private final JButton editButton = new JButton("Edit");
	// private final JButton profileButton = new JButton("Profile");
	private final JButton addButton = new JButton("Add");
	private final JButton removeButton = new JButton("Remove");

	private JTable table; // << table will eventually hold a reference to the

	private UserTableModel userModel;

	public UsersTab(JFrame owner)
	{
		super();
		setButtonActions(owner);
		userModel = new UserTableModel();

		AutoTeamTablePanel theTablePanel = new AutoTeamTablePanel(userModel,
				editButton, addButton, removeButton);
		table = theTablePanel.getTable();

		this.add(theTablePanel);
	}

	@Override
	protected void setButtonActions(final JFrame parentFrame)
	{
		addButton.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				UserCreator dialog = new UserCreator(parentFrame,userModel);
				dialog.setVisible(true);
			}
		});

		editButton.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				// TODO remove setCurrentFrame after integration is Complete
				// RSMSController.setCurrentFrame( new JFrame() );
				UserEditor dialog = new UserEditor(parentFrame, userModel,
						table.getSelectedRow());
				dialog.setVisible(true);
			}
		});
		removeButton.addActionListener( new ActionListener() 
		{

			@Override
			public void actionPerformed(ActionEvent e) 
			{
				userModel.delete(table.getSelectedRow());
			}
			
		});

	}
}
